package com.imut.onlinebanking.xrh.controller;


import com.imut.onlinebanking.base.response.JsonResult;
import com.imut.onlinebanking.base.response.StatusCode;
import com.imut.onlinebanking.xrh.pojo.entity.Account;
import com.imut.onlinebanking.xrh.service.xrhSer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TransferController {

    @Autowired
    private xrhSer xrhService;

    // 获取所有账户列表
    @GetMapping("/accounts")
    public JsonResult getAccountList() {
        try {
            List<Account> accounts = xrhService.selectByAccount();
            return JsonResult.ok(accounts);
        } catch (Exception e) {
            return JsonResult.fail(StatusCode.OPERATION_FAILED);
        }
    }

    // 处理转账请求
    @PostMapping("/transfer")
    public JsonResult transfer(@RequestBody Map<String, Object> request) {
        try {
            // 获取参数
            String fromAccountId = (String) request.get("fromAccountId");
            String toAccountId = (String) request.get("toAccountId");
            String amount =request.get("amount").toString();
            String remark = (String) request.get("remark");

            // 验证参数
            if (fromAccountId == null || toAccountId == null || amount == null) {
                return JsonResult.fail(StatusCode.OPERATION_FAILED);
            }

            if (fromAccountId.equals(toAccountId)) {
                return new JsonResult(StatusCode.OPERATION_FAILED.getCode(), "转出账户和转入账户不能相同");
            }

            if (Integer.parseInt(amount) <= 0) {
                return new JsonResult(StatusCode.OPERATION_FAILED.getCode(), "转账金额必须大于0");
            }

            // 获取转出账户信息
            List<Account> fromAccounts = xrhService.selectByAccountId(fromAccountId);
            if (fromAccounts == null || fromAccounts.isEmpty()) {
                return new JsonResult(StatusCode.OPERATION_FAILED.getCode(), "转出账户不存在");
            }
            Account fromAccount = fromAccounts.get(0);

            // 检查余额是否足够
            Double fromBalance =fromAccount.getBalance();
            if (Integer.parseInt(amount)  < 0) {
                return new JsonResult(StatusCode.OPERATION_FAILED.getCode(), "账户余额不足");
            }

            // 获取转入账户信息
            List<Account> toAccounts = xrhService.selectByAccountId(toAccountId);
            if (toAccounts == null || toAccounts.isEmpty()) {
                return new JsonResult(StatusCode.OPERATION_FAILED.getCode(), "转入账户不存在");
            }
            Account toAccount = toAccounts.get(0);

            // 计算新余额
            Double newFromBalance = fromBalance-Integer.parseInt(amount);
            BigDecimal newToBalance = BigDecimal.valueOf(toAccount.getBalance()+Integer.parseInt(amount));

            // 执行转账操作
            int fromResult = xrhService.updateBalance(fromAccountId, newFromBalance.toString());
            int toResult = xrhService.updateBalance(toAccountId, newToBalance.toString());

            if (fromResult > 0 && toResult > 0) {
                Map<String, Object> result = new HashMap<>();
                result.put("fromAccountId", fromAccountId);
                result.put("toAccountId", toAccountId);
                result.put("amount", amount);
                result.put("remark", remark);
                result.put("newFromBalance", newFromBalance);
                result.put("newToBalance", newToBalance);
                return JsonResult.ok(result);
            } else {
                return JsonResult.fail(StatusCode.OPERATION_FAILED);
            }
        } catch (Exception e) {
            return JsonResult.fail(StatusCode.OPERATION_FAILED);
        }
    }
}